package cn.jjxx.shiro.websocket;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

import cn.jjxx.core.utils.PropertiesUtil;
import cn.jjxx.modules.sys.service.impl.AttachmentServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  上传文件
 * </p>
 */
@Transactional
@Configuration
public class UploadUtils {

	public static final String DEFAULT_CONFIG_FILE = "upload.properties";

	/**
	 * 
	 *@Title UploadUtils.java
	 *@description TODO
	 *@time 2021年3月11日 上午9:54:02
	 *@author YAOXIONG
	 *@version 1.0
	 *
	 */
	protected static String getbaseserverPath() { 
		PropertiesUtil propertiesUtil = new PropertiesUtil(AttachmentServiceImpl.DEFAULT_CONFIG_FILE);
		String baseserverPath  = propertiesUtil.getString("upload.base.server.path");
		return baseserverPath;
	}

	/**
	 * 
	 *@Title UploadUtils.java
	 *@description TODO
	 *@time 2021年3月11日 上午9:53:22
	 *@author YAOXIONG
	 *@version 1.0
	 *
	 */
	protected static final String datePath() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyy/MM/dd/");
	}
	
	 
	/**
	 * @Author zz
	 * @Description 上传文件
	 * @param
	 * @return
	 */
	public static JSONObject uploadFile(HttpServletRequest request, MultipartFile file) {
		JSONObject result = new JSONObject();
		try {
			//判断文件是否为空
			if (file.isEmpty()) {
				result.put("success", false);
				result.put("message", "文件为空");
			}
			long fileSize = file.getSize();
			if(fileSize > 314572800) {
				result.put("success", false);
				result.put("message", "文件不能超过300MB");
			}
			String fileName = file.getOriginalFilename(); 
			fileName = fileName.substring(fileName.length()-8);
			String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ fileName;
			newFileName = "upload\\"+datePath() + newFileName;
			String path = getbaseserverPath() + "\\"+newFileName;
			File checkFile = new File(path);
			if(checkFile.exists()){
				result.put("success", false);
				result.put("message", "文件已存在");
			}
			if (!checkFile.getParentFile().exists()) {
				checkFile.getParentFile().mkdir();
			} 
			//保存文件
			file.transferTo(checkFile);
			result.put("success", true);
			result.put("message", "上传成功");
			result.put("fileName", newFileName);
		} catch (IOException e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("message", "上传失败");
		}
		return result;
	}

	/**
	 * @Author zz
	 * @Description 删除文件
	 * @param
	 * @return
	 */
	public static JSONObject deleteFile(String fileName) {
		JSONObject result = new JSONObject(); 
		String path = getbaseserverPath() +"\\"+ fileName;
		File file = new File(path);
		boolean flag = false;
		if (!file.exists()) {
			result.put("message", "文件不存在");
			result.put("success", flag);
		}
		try{
			flag = file.delete();
		}catch (Exception e){
			e.printStackTrace();
		}
		result.put("message", "删除成功");
		result.put("success", flag);
		return result;
	}

}
